<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use think\Image;
class Tianqi extends Controller
{
    public function index(Request $request){
        $p = $request->get();
        // var_dump( $p['area']);die;
        header("Content-Type:text/html;charset=UTF-8");
        date_default_timezone_set("PRC");
        $showapi_appid = '41405';  //替换此值,在官网的"我的应用"中找到相关值
        $cityName = empty($p['area'])?'上海':$p['area'];
        $showapi_secret = '860e8b5f019d4057b2f0ec528c2f02eb';  //替换此值,在官网的"我的应用"中找到相关值
        $paramArr = array(
             'showapi_appid'=> $showapi_appid,
             'areaid'=> "",
             'area'=> $cityName,
             'needMoreDay'=> "",
             'needIndex'=> "",
             'needHourData'=> "",
             'need3HourForcast'=> "",
             'needAlarm'=> ""
             //添加其他参数
        );
         // var_dump($paramArr);die;
        //创建参数(包括签名的处理)
        function createParam ($paramArr,$showapi_secret) {
             $paraStr = "";
             $signStr = "";
             ksort($paramArr);
             foreach ($paramArr as $key => $val) {
                 if ($key != '' && $val != '') {
                     $signStr .= $key.$val;
                     $paraStr .= $key.'='.urlencode($val).'&';
                 }
             }
             $signStr .= $showapi_secret;//排好序的参数加上secret,进行md5
             $sign = strtolower(md5($signStr));
             $paraStr .= 'showapi_sign='.$sign;//将md5后的值作为参数,便于服务器的效验
             // echo "排好序的参数:".$signStr."<br>\r\n";
             return $paraStr;
        }
         
        $param = createParam($paramArr,$showapi_secret);
        $url = 'http://route.showapi.com/9-2?'.$param; 
        // var_dump($url);die;
        // echo "请求的url:".$url."<br>\r\n";
        $result = file_get_contents($url);
        // echo "返回的json数据:<br>\r\n";
        // print $result.'<br>\r\n';
        $result = json_decode($result);
        // var_dump($result);die;
        $list = $result->showapi_res_body->cityInfo;
        $list2 = $result->showapi_res_body->now->aqiDetail;
        $list3 = $result->showapi_res_body->f1;
        // var_dump($result);
        // echo "<br>\r\n取出showapi_res_code的值:<br>\r\n";
        // print_r($result->showapi_res_code);
        // echo "<br>\r\n";
        return view('tianqi/index',[
            'list' => $list,
            'list2' => $list2,
            'list3' => $list3,
            ]);
    }

    // public function add(Request $request){
    //     $p = $request->get();

    //     var_dump($p);die;
    // }
}
    